# -*- coding: utf-8 -*-
"""
Created on Sat Oct 24 17:41:16 2020

@author: WuZhen
"""

import re
import json
import matplotlib.pyplot as plt
import pandas as pd

s = 'Best Valid rmse= \d+.\d+, Best Test rmse= \d+.\d+, Patience=\d+, Time=\d+.\d+'

output_file = 'output.txt'
result_file = 'result.json'
rs = []


def result_to_dict():
    res = []
    opt = ''''''
    with open(output_file, 'r', errors='ignore', encoding="utf-16") as f:
        for line in f.readlines():
            t = line.strip()
            if len(t) > 0:
                opt += t
    for m in re.findall(s, opt.strip()):
        it = {}
        for j in m.split(", "):
            x = j.split("=")
            it[x[0]] = float(x[1])
        res.append(it)
    return res


def save(data):
    with open(result_file, 'w+') as f:
        json.dump(data, f)


result = result_to_dict()
# save(result)



df = pd.DataFrame(result)
df[['Best Test rmse', 'Best Valid rmse', 'Patience']].plot(secondary_y='Patience',
                                                           figsize=(16, 9),
                                                           xlabel="training epochs",
                                                           ylabel="rmse"
                                                           )

plt.show()
